package com.D5Power.Editor.EditorWindow
{
	import com.D5Power.Editor.Event.ClickObjectEvent;
	import com.D5Power.Objects.BuildingObject;
	import com.D5Power.Objects.GameObject;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import org.aswing.JCheckBox;
	import org.aswing.JComboBox;
	import org.aswing.JLabel;
	import org.aswing.JPanel;
	import org.aswing.JTextField;
	import org.aswing.ext.Form;
	
	public class BuildingAttr extends ObjectAttr
	{
		private var _hidden_point:JTextField;
		private var _set_both:JCheckBox;
		private var _checkHit:JCheckBox;
		private var _zorder:JTextField;
		
		/**
		 * @param	owner	主程序映射
		 * @param	obj		控制对象
		 */ 
		public function BuildingAttr(owner:MapEditor, obj:GameObject=null)
		{
			mytitle='建筑属性';
			super(owner, obj);
		}
		
		override protected function init():void
		{
			
			_box = new Form();
			
			// 增加循环背景控制
			_hidden_point = new JTextField(TrueObj.zero.x+','+TrueObj.zero.y,10);
			_set_both = new JCheckBox('批量');
			var lable:JLabel = new JLabel('消隐点：',null,JLabel.LEFT);
			
			_zorder = new JTextField(TrueObj.zOrderF.toString());
			var lable2:JLabel = new JLabel('排序浮动：',null,JLabel.LEFT);
			
			_checkHit = new JCheckBox('参与碰撞');
			var lable3:JLabel = new JLabel('碰撞设置：');
			_checkHit.setSelected(TrueObj.canBeAtk);
						
			_box.addRow(lable,_hidden_point,_set_both);
			_box.addRow(lable2,_zorder);
			_box.addRow(lable3,_checkHit);

			_box.setSizeWH(200,120);
			getContentPane().append(_box);
			
			super.init();
			
		}
		
		override protected function onSubmit(e:Event):void
		{
			var arr:Array = _hidden_point.getText().split(',');
			if(arr.length<2) return;
			
			
			if(_set_both.isSelected())
			{
				// 批量查询同素材的全部目标，并进行设置
				var obj_arr:Array = _root.Shower.scene.getAllObjects();
				for each(var obj:GameObject in obj_arr)
				{
					if(obj.graphics.resName!=TrueObj.graphics.resName) continue;
					(obj as BuildingObject).zero = new Point(int(arr[0]),int(arr[1]));
				}
			}else{
				TrueObj.zero = new Point(int(arr[0]),int(arr[1]));
			}
			
			TrueObj.canBeAtk = _checkHit.isSelected();
			TrueObj.zOrderF = int(_zorder.getText());
			
			super.onSubmit(e);
		}
		
		private function get TrueObj():BuildingObject
		{
			return _obj as BuildingObject;
		}
		
		/**
		 * 鼠标事件
		 */ 
		override public function onMouseEvent(e:ClickObjectEvent):void
		{
			var d:GameObject = e.click_target;
			var tpos:Point = _root.Shower.scene.Map.getWorldPostion(e.sceneX,e.sceneY);
			
			var tx:int= Math.floor(tpos.x-(d.Pos.x-d.graphics.frameWidth/2));
			var ty:int= Math.floor(tpos.y-(d.Pos.y-d.graphics.frameHeight));
			
			if(tx>d.graphics.frameWidth || tx<0) return;
			if(ty>d.graphics.frameHeight || ty<0) return;
			
			_hidden_point.setText(tx+','+ty);
		}
		
		
	}
}